package com.zw.yzk.kabu.data.repository;


import com.zw.yzk.kabu.data.constant.Constant;
import com.zw.yzk.kabu.data.network.ServiceGenerator;
import com.zw.yzk.kabu.data.network.entity.SearchTopicEntity;
import com.zw.yzk.kabu.data.network.service.SearchTopicService;
import com.zw.yzk.kabu.data.network.task.SearchTopicTask;
import com.zw.yzk.kabu.data.network.task.Task;

import javax.inject.Inject;

import rx.Observable;
import rx.functions.Func1;

/**
 * Created by zhanwei on 2017/1/4.
 */

public class SearchTopicRepository extends CommonRepository {

    private int total;
    @Inject
    public SearchTopicRepository() {
        total = SearchTopicTask.DEFALUT_COUNT_PER_PAGE;
    }

    @Override
    protected Observable connectServer(Task task) {
        SearchTopicService service = ServiceGenerator.createService(SearchTopicService.class);
        SearchTopicTask topicTask = (SearchTopicTask) task;
//        if(topicTask.start >= total) {
//            SearchTopicEntity entity = new SearchTopicEntity();
//            entity.errCode = Constant.RESULT_OK;
//            entity.result.total = total;
//
//            return Observable.just(entity);
//        }
        return service.searchExhibition(topicTask.start, 10, topicTask.key);
    }

    @Override
    protected Func1<SearchTopicEntity, Observable> getServerData() {
        return new Func1<SearchTopicEntity, Observable>() {
            @Override
            public Observable call(SearchTopicEntity entity) {
                total = entity.result.total;
                return Observable.just(entity);
            }
        };
    }

}
